package edu.hnui.shop.service.impl;

import edu.hnui.shop.commons.ResultCodeEnum;
import edu.hnui.shop.exception.ShopBusinessException;
import edu.hnui.shop.mapper.UserMapper;
import edu.hnui.shop.pojo.User;
import edu.hnui.shop.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

@Service
@Transactional  //事务管理 （ 隔离属性、传播属性）
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User findById(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        if(Objects.isNull(user)){
            throw new ShopBusinessException(ResultCodeEnum.NULL_POINT);
        }
        return user;
    }


    /**
     * 实现具体的业务逻辑
     * @param telphone
     * @param pwd
     * @return
     */
    public User login(String telphone,String pwd){

       User user=  userMapper.queryByTelphoneAndPwd(telphone,pwd);
       if(Objects.isNull(user)){
           //抛出异常
           throw new ShopBusinessException(ResultCodeEnum.TELPHONE_OR_PWD_ERROR);
       }

       return user;

    }
}
